package tw.com.softleader.jerseyexam.users;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;


public class UserService {
	
	private Map<Integer, UserModel> userMap = new HashMap<Integer, UserModel>();
	
	public UserService() {
		UserModel user1 = new UserModel();
		user1.setId(1);
		user1.setUsername("gary");
		userMap.put(1, user1);
		
		UserModel user2 = new UserModel();
		user2.setId(2);
		user2.setUsername("jack");
		userMap.put(2, user2);
		
		UserModel user3 = new UserModel();
		user3.setId(3);
		user3.setUsername("namoi");
		userMap.put(3, user3);
		
		UserModel user4 = new UserModel();
		user4.setId(4);
		user4.setUsername("stacey");
		userMap.put(4, user4);
		
		UserModel user5 = new UserModel();
		user5.setId(5);
		user5.setUsername("matt");
		userMap.put(5, user5);
		
	}
	
	public Collection<UserModel> getAll() {
		Collection<UserModel> users = new ArrayList<UserModel>();
		for(Integer i : userMap.keySet()) {
			users.add(userMap.get(i));
		}
		return users;
	}
	
	public UserModel getOne(Integer id) {
		return userMap.get(id);
	}
	
	public void insert(UserModel user) {
		userMap.put(user.getId(), user);
	}

	public void update(UserModel user) {
		// map will replace older by new one
		userMap.put(user.getId(), user);
	}
	
	public void delete(Integer id) {
		userMap.remove(id);
	}
	
}
